package com.army.study.widget.widgetbus;

import com.army.study.MainAbility;
import com.army.study.database.Bus;
import com.army.study.database.BusComesDatabase;
import com.army.study.database.Form;
import com.army.study.database.Station;
import com.army.study.service.TimerAbility;
import com.army.study.utils.DatabaseUtils;
import com.army.study.utils.LogUtils;
import com.army.study.widget.controller.FormController;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;

import java.util.ArrayList;
import java.util.List;


public class WidgetBusImpl extends FormController {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, WidgetBusImpl.class.getName());
    private static final int DIMENSION_1X2 = 1;
    private static final int DIMENSION_2X2 = 2;
    private static final int DIMENSION_2X4 = 3;
    private static final int DIMENSION_4X4 = 4;

    // 车站标签之间距离
    private int stationWidth = 40;
    // 当前公交车所在车站
    private int currentStation = 0;
    // 候车站
    private int waitingStation = 0;
    // 车站数
    private int stationSize = 12;

    private DatabaseHelper helper = new DatabaseHelper((MainAbility)context);
    private OrmContext connect;

    public WidgetBusImpl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
    }

    @Override
    public ProviderFormInfo bindFormData() {
        HiLog.info(TAG, "bind form data");
        ZSONObject zsonObject = new ZSONObject();
        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        if (dimension == DIMENSION_1X2) {
            zsonObject.put("mini", true);
            zsonObject.put("detailStation", false);
        }
        if (dimension == DIMENSION_2X2) {
            zsonObject.put("imagePaddingTop", "12px");
            currentStation = 1;
            waitingStation = 10;
            float basePercent = stationWidth / (stationSize * stationWidth / 100.0F);
            float currentPercent = basePercent * currentStation;
            float secondaryPercent = basePercent * waitingStation;

            zsonObject.put("detailStation", true);
            zsonObject.put("detailContent", "目前: 三滘村 - 候车站: 塘步西");
            zsonObject.put("currentPercent", currentPercent);
            zsonObject.put("secondaryPercent", secondaryPercent);
        }
        if (dimension == DIMENSION_2X4) {
            zsonObject.put("imagePaddingTop", "12px");
            currentStation = 2;
            waitingStation = 10;
            float basePercent = stationWidth / (stationSize * stationWidth / 100.0F);
            float currentPercent = basePercent * currentStation;
            float secondaryPercent = basePercent * waitingStation;

            zsonObject.put("detailStation", true);
            zsonObject.put("detailContent", "目前: 沥滘 - 候车站: 华南碧桂园");
            zsonObject.put("currentPercent", currentPercent);
            zsonObject.put("secondaryPercent", secondaryPercent);
        }
        if (dimension == DIMENSION_4X4) {
            zsonObject.put("imagePaddingTop", "12px");
            currentStation = 2;
            waitingStation = 11;
            float basePercent = stationWidth / (stationSize * stationWidth / 100.0F);
            float currentPercent = basePercent * currentStation;
            float secondaryPercent = basePercent * waitingStation;

            zsonObject.put("detailStation", true);
            zsonObject.put("detailContent", "目前: 后滘村 - 候车站: 万博翠湖花园");
            zsonObject.put("currentPercent", currentPercent);
            zsonObject.put("secondaryPercent", secondaryPercent);
        }



        providerFormInfo.setJsBindingData(new FormBindingData(zsonObject));
        return providerFormInfo;
    }

    @Override
    public void updateFormData(long formId, Object... vars) {
        HiLog.info(TAG, "update form data timing, default 30 minutes");
        System.out.println(formId + " ****** Update");
        System.out.println(vars.length);
        HiLog.info(TAG, formId + " ****** Update");
        if (connect == null) {
            connect = helper.getOrmContext("BusComesDatabase", "BusComesDatabase.db", BusComesDatabase.class);
        }
        // 1. 先获取卡片信息
        Form form = DatabaseUtils.getFormData(connect, formId);
        // 1.1 如果是编辑卡片, 要更改卡片和公交车Id
        Long busId = form.getBusId();
        if (vars.length > 0) {
            System.out.println((Long) vars[0]);
            busId = (Long) vars[0];
            // 重新绑定卡片与公交车
            form.setBusId(busId);
            DatabaseUtils.updateForm(connect, form);
            currentStation = 0;
            waitingStation = 11;
        }
        System.out.println(formId + " ****** " + busId);
        // 2. 再根据公交Id获取公交行走路线站点信息
        Bus bus = DatabaseUtils.getBusById(connect, busId);
        List<Station> stationDetail = DatabaseUtils.getStationByParentId(connect, busId);
        System.out.println("stationDetail size: " + stationDetail.size());
        // 3. 封装返回卡片需要信息数据
        ZSONObject zsonObject = new ZSONObject();

        ZSONArray zsonArray = new ZSONArray();
        for (Station obj : stationDetail) {
            ZSONObject station = new ZSONObject();
            station.put("stationName",obj.getStationName());
            station.put("parentId",obj.getParentId());
            station.put("displayOrder",obj.getDisplayOrder());
            zsonArray.add(station);
        }

        zsonObject.put("stationDetail", zsonArray);
        System.out.println("zsonArray size: " + zsonArray.size());


        // 模拟每次更新前进一站
        currentStation++;

        stationSize = stationDetail.size()-1;
        if (currentStation >= waitingStation) {
            currentStation = waitingStation;
        }
        if (currentStation >= stationSize) {
            currentStation = stationSize;
        }

        float basePercent = stationWidth / (stationSize * stationWidth / 100.0F);
        float currentPercent = basePercent * currentStation;
        float secondaryPercent = basePercent * waitingStation;

        Station currentObj = stationDetail.get(currentStation);
        Station waitingObj = stationDetail.get(waitingStation);

        zsonObject.put("itemTitle", bus.getBusNum());
        zsonObject.put("itemContent", bus.getWaitingStation() + " - " + bus.getEndStation());
        zsonObject.put("detailContent", "目前: "+currentObj.getStationName()+" - 候车站: "+waitingObj.getStationName());
        zsonObject.put("currentPercent", currentPercent);
        zsonObject.put("secondaryPercent", secondaryPercent);

        System.out.println("Size: "+stationDetail.size()+", stationWidth: "+stationWidth+", currentPercent: "+currentPercent+
                ", secondaryPercent: "+secondaryPercent+", currentStation: "+currentStation+", waitingStation: "+waitingStation + ", basePercent: "+basePercent);

        System.out.println("zsonObject: " + ZSONObject.toZSONString(zsonObject));
        FormBindingData formBindingData = new FormBindingData(zsonObject);
        try {
            // 更新卡片信息
            ((MainAbility)context).updateForm(formId, formBindingData);
        } catch (FormException e) {
            e.printStackTrace();
        }
        System.out.println(formId + " ------------------------- " + busId);
    }

    @Override
    public void onTriggerFormEvent(long formId, String message) {
        HiLog.info(TAG, "handle card click event.");
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        HiLog.info(TAG, "get the default page to route when you click card.");
        return null;
    }
}
